﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.SessionState;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class GioHang : System.Web.UI.Page
    {
        ShopingCart vpp = new ShopingCart();
        private static DataTable tb = new DataTable();
        private static int[] idgh = new int[100];
        private static int tong = 0;
        private float total = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadGioHang();
                loadNB();
            }
        }
        public void Calc(object sender, EventArgs e)
        {
            TextBox tb1 = sender as TextBox;
            GridViewRow row = (GridViewRow)tb1.NamingContainer;
            string strCommissionPayoutGross = (row.FindControl("txtSL") as TextBox).Text;
            Label DG = (Label)row.FindControl("lbDG");
            Label GG = (Label)row.FindControl("lbDGG");

            decimal sum = 0;

            int sl = int.Parse(tb1.Text);
            int gg = int.Parse(GG.Text.Replace("%", "").Trim());
            decimal dg = Decimal.Parse(DG.Text.Replace(" VNĐ", "").Replace(".", "").Trim());

            Label TT = (Label)row.FindControl("lbThanhTien");
            decimal ThanhTien = (decimal)sl * (dg - ((dg * gg) / 100));
            TT.Text = (sl * (dg - ((dg * gg) / 100))).ToString("##,## VNĐ");

            for (int i = 0; i < GridGioHang.Rows.Count; i++)
            {
                Label TT1 = (Label)GridGioHang.Rows[i].FindControl("lbThanhTien");
                Decimal t = Decimal.Parse(TT1.Text.Replace(" VNĐ", "").Replace(".", "").Trim());
                sum += t;
            }

            DG.Text = dg.ToString("##,## VNÐ");
            lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNĐ") + "</span>";
            lbThanhTien.Text = "Tổng: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " VNĐ") + "</span";


            tb = vpp.upDateSL(tb, Int32.Parse(((Label)row.FindControl("lbSTT")).Text), sl, ThanhTien);
        }
        public void loadNB()
        {
            bindatalist(new cmsSanPhamBL().SelectAllBanChay());
        }
        protected void loadGioHang()
        {

            if (Session["GioHang"] != null)
            {
                tb = (DataTable)Session["GioHang"];
                DataTable dt = new DataTable();
                dt.Columns.Add("STT");
                dt.Columns.Add("SanPhamID");
                dt.Columns.Add("TenSanPham");
                dt.Columns.Add("HinhAnh");
                dt.Columns.Add("SoLuong");
                dt.Columns.Add("DonGia");
                dt.Columns.Add("GiamGia");
                dt.Columns.Add("ThanhTien");

                int i = 0;
                foreach (DataRow dr in tb.Rows)
                {
                    cmsSanPhamDO obj = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = Int64.Parse(dr[1].ToString()) });
                    i++;
                    DataRow row = dt.NewRow();
                    row[0] = dr[0];
                    row[1] = dr[1];
                    row[2] = obj.TenSanPham;
                    row[3] = obj.HinhAnh;
                    row[4] = dr[2];
                    row[5] = obj.DonGia;
                    row[6] = obj.GiamGia;
                    row[7] = dr[3];
                    dt.Rows.Add(row);
                }

                if (dt.Rows.Count > 0)
                {
                    GridGioHang.DataSource = dt;
                    GridGioHang.DataBind();
                    float sum = 0;
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        sum += float.Parse(dt.Rows[j].ItemArray[7].ToString());
                    }
                    lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNĐ") + "</span>";
                    lbThanhTien.Text = "Tổng: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";
                }
            }
            else
            {
                lbl.Text = "Chua có sản phẩm nào trong giỏ hàng";
                lblSum.Text = "";
            }
        }
        protected void GridGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Int64 id = Int64.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "xoa")
            {
                new cmsGioHangBL().Delete(new cmsGioHangDO { GioHangID = id });
                Response.Redirect("/GioHang.aspx");
            }
            if (e.CommandName == "capnhat")
            {
                cmsGioHangDO obj = new cmsGioHangDO();
                for (int i = 0; i < GridGioHang.Rows.Count; i++)
                {
                    TextBox lbSL = (TextBox)GridGioHang.Rows[i].FindControl("txtSL");

                    obj = new cmsGioHangBL().Select(new cmsGioHangDO { GioHangID = Int64.Parse(e.CommandArgument.ToString()) });
                    cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = obj.SanPhamID });
                    float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);
                    try
                    {
                        obj.SoLuong = int.Parse(lbSL.Text);
                        obj.ThanhTien = (float)gia * obj.SoLuong;
                    }
                    catch { }
                    new cmsGioHangBL().Update(obj);
                }
                Response.Redirect("/GioHang.aspx");
            }
        }
        protected void GridGioHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
        protected void GridGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Page.Title = "Giỏ hàng - Công ty thiết bị cổ phần y tế Bảo Anh";
           //      Page.Title = "Giỏ hàng - Văn phòng phẩm Mỹ Ðình";
                decimal sum = 0;
                TextBox lbSL = (TextBox)e.Row.FindControl("txtSL");
                Label DG = (Label)e.Row.FindControl("lbDG");
                Label GG = (Label)e.Row.FindControl("lbDGG");

                int sl = int.Parse(lbSL.Text);
                int gg = int.Parse(GG.Text.Replace("%", "").Trim());
                string tem = DG.Text.Replace("VNÐ", "").Trim();
                tem = tem.Replace(".", "");
                decimal dg = Decimal.Parse(tem);

                Label TT = (Label)e.Row.FindControl("lbThanhTien");
                TT.Text = (sl * (dg - ((dg * gg) / 100))).ToString("##,## VNÐ");

                for (int i = 0; i < GridGioHang.Rows.Count; i++)
                {
                    Label TT1 = (Label)GridGioHang.Rows[i].FindControl("lbThanhTien");
                    sum += Decimal.Parse(TT1.Text.Replace(" VNÐ", "").Replace(".", "").Trim());
                }

                DG.Text = dg.ToString("##,## VNÐ");
                lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNÐ") + "</span>";
                lbThanhTien.Text = "Tổng: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("style", "background-color: #FFFFFF; color: black;");
                e.Row.Attributes.Add("onmouseover", "style.backgroundColor='#DEE0BF'");
                e.Row.Attributes.Add("onmouseout", "style.backgroundColor='#FFFFFF'");
            }
            //catch { }
            
        }
        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            for (int i = 0; i < GridGioHang.Rows.Count; i++)
            {
                TextBox lbSL = (TextBox)GridGioHang.Rows[i].FindControl("txtSL");
                //int valuuu = (int)GridGioHang[i, 2].Value;
                obj = new cmsGioHangBL().Select(new cmsGioHangDO { GioHangID = Int64.Parse(GridGioHang.DataKeys[i].Value.ToString()) });
                try
                {
                    obj.SoLuong = int.Parse(lbSL.Text);
                }
                catch { }
                new cmsGioHangBL().Update(obj);
            }
            loadGioHang();
        }
        protected void GridGioHang_DataBinding(object sender, System.EventArgs e)
        {

        }
        protected void GridGioHang_DataBound(object sender, EventArgs e)
        {

        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public cmsGioHangDO init()
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            if (Session["ClientLoginID"] == null)
                obj.ClientLoginID = 0;
            else
                obj.ClientLoginID = int.Parse(Session["ClientLoginID"].ToString());
            obj.NgayDatHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.NgayGiaoHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.SoLuong = 1;
            obj.XuLy = false;
            return obj;
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rpt.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 8;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rpt.Visible = true;
                rpt.DataSource = objPds;
                rpt.DataBind();
            }
            else
            {
                lbThongbao.Visible = true;
                lbThongbao.Text = "Không có sản phẩm!";
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            loadNB();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            loadNB();
        }
    }
}
